﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataBaseAccessLib;
using BusinessModel.EntityModel;
using BusinessModel;

namespace SDZJ.ManagerView.SystemManager
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        #region********变量定义**************
        /// <summary>
        /// 当前页面
        /// </summary>
        public int currentPage = 1;

        /// <summary>
        /// 查询条件
        /// </summary>
        public string condition = "";
        /// <summary>
        /// 实例化操作类
        /// </summary>
        //MaterialHelper cMaterailList = new MaterialHelper();
        /// <summary>
        ///日志信息实体
        /// </summary>
        //LogInfo logInfo = new LogInfo();
        /// <summary>
        ///日志信息操作类
        /// </summary>
       // LogHelper cLog = new LogHelper();
        /// <summary>
        /// 用户id  
        /// </summary>
        public string currentUserID = "";
        /// <summary>
        /// 用户名称
        /// </summary>
        private string currentUserName = "";
        #endregion***************************
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDataSource(condition);
            }
        }
      //#region******方法**********
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="condition"></param>
        private void GetDataSource(String condition)
        {
            //排序字段
            string sortExpression = "userid";
            //排序方式
            string sortDirection = "";
            if (this.gvInfo.Attributes["SortExpression"] != null)
            {
                sortExpression = this.gvInfo.Attributes["SortExpression"];
                sortDirection = this.gvInfo.Attributes["SortDirection"];
            }
            this.AspNetPager1.PageChanged -= AspNetPager_PageChanged;

           // condition = ""; //" where companyType like '%" + tbxQuery.Text + "%' or materialType like '%" + tbxQuery.Text + "%' or materialContent like '%" + tbxQuery.Text + "%'";

            DataTable table = GetAllUserList(AspNetPager1.PageSize, currentPage, AspNetPager1, sortExpression, sortDirection, condition);
            if (table != null)
            {
                gvInfo.DataSource = table.DefaultView;
                gvInfo.DataBind();
            }

            if (currentPage > 1)
            {
                AspNetPager1.FirstPageText = "<img src=../../images/page02.gif border=0 valign=middle hspace=3>";
                AspNetPager1.PrevPageText = "<img src=../../images/page01.gif border=0 valign=middle hspace=3>";
            }
            else
            {
                AspNetPager1.FirstPageText = "<img src=../../images/page05.gif border=0 valign=middle hspace=3>";
                AspNetPager1.PrevPageText = "<img src=../../images/page06.gif border=0 valign=middle hspace=3>";
            }

            if (currentPage == AspNetPager1.PageCount || AspNetPager1.PageCount == 0)
            {
                AspNetPager1.LastPageText = "<img src=../../images/page08.gif border=0 valign=middle hspace=3>";
                AspNetPager1.NextPageText = "<img src=../../images/page07.gif border=0 valign=middle hspace=3>";
            }
            else
            {
                AspNetPager1.LastPageText = "<img src=../../images/page04.gif border=0 valign=middle hspace=3>";
                AspNetPager1.NextPageText = "<img src=../../images/page03.gif border=0 valign=middle hspace=3>";
            }

            this.AspNetPager1.PageChanged += AspNetPager_PageChanged;
        }
        //gridview行点击事件
        protected override void Render(HtmlTextWriter writer)
        {
            //string postSc = ClientScript.GetPostBackEventReference(GridView1, "Select$" + row.RowIndex.ToString(), true);
            //e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this);" + postSc + "");//点击行变色

            foreach (GridViewRow row in this.gvInfo.Rows)
            {

                if (row.RowType == DataControlRowType.DataRow)
                {
                    //  row.Attributes.Add("onclick", postSc + ";" + row.ClientID.ToString() + ".checked=true;selectx(this);");//点击行变色
                    string postSc = ClientScript.GetPostBackEventReference(this.gvInfo, "Select$" + row.RowIndex.ToString(), true);
                    row.Attributes.Add("onclick", postSc + ";");
                }
            }
            base.Render(writer);

        }
        /// <summary>
        /// 分页控件翻页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            currentPage = AspNetPager1.CurrentPageIndex; // 记录列表当前页码
            txtCurrentPage.Value = currentPage.ToString();
            GetDataSource(condition);   // 绑定所有信息
        }
        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression.ToString();
            string sortDirection = "ASC";
            if (sortExpression == this.gvInfo.Attributes["SortExpression"])
            {
                sortDirection = (this.gvInfo.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }
            this.gvInfo.Attributes["SortExpression"] = sortExpression;
            this.gvInfo.Attributes["SortDirection"] = sortDirection;
            GetDataSource(condition);
        }
        /// <summary>
        /// 删除事件
        /// </summary>   
        protected void gvInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
        }
        /// <summary>
        /// 行绑定事件
        /// </summary>   
        protected void gvInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               // e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");//点击行变色
               // e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this);javascirpt:__doPostBack('gvInfo','Select$" + e.Row.RowIndex + "')");//点击行变色

                //GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text;
                //GridView1.SelectedDataKey.Values[1]

            }
        }
        public DataTable GetAllUserList(int pageSize, int currentPage, Wuqi.Webdiyer.AspNetPager pager, string sortExpression, string sortDirection, string condition)
        {
            int totalPage = 0, totalRow = 0;//总页数，总记录数
            SQLScript sqlScript = new SQLScript();
            DataTable table = null;

            try
            {
                //if (mc.KeyExist("UserList" + currentPage))
                //{
                //    //获取
                //    table = (DataTable)mc.GetObject("materialList" + currentPage);
                //    totalRow = (int)mc.GetObject("materialListTotalRow");
                //}
                //else
               // {
                    IDBAccess DB = DBFactory.GetDBAccess(OConfig.SysDBType, OConfig.DBConnection);
                    int startIndex, endIndex;
                    string strCol = " USERID,USERNAME,NICENAME,DESCRIPTION,REG_DATE,DISABLED,LOCKED ";
                    OConfig.GetStartRowNum(" TS_USER a ", condition, currentPage, pageSize, out startIndex, out endIndex, out totalPage, out totalRow);
                    string sqlCommand = sqlScript.QueryByDividePage(" TS_USER a ", condition, strCol, sortExpression + " " + sortDirection, startIndex, endIndex);
                    table = DB.ExecuteDataTable(sqlCommand, CommandType.Text, null);

                    //存储
                    //mc.SetObject("materialList" + currentPage, table);
                    //mc.SetObject("materialListTotalRow", totalRow);
               // }

                pager.RecordCount = totalRow;
                if (currentPage > pager.PageCount)
                {
                    currentPage = pager.PageCount;
                }
                if (currentPage == 0)
                {
                    currentPage = 1;
                }
                pager.CurrentPageIndex = currentPage;
            }
            catch (Exception exp)
            {
                table = null;
            }
            finally
            {
                pager.CustomInfoHTML = "共<font color=red>" + pager.RecordCount + "</font>条数据&nbsp;&nbsp;&nbsp;&nbsp;当前<font color=red>" + pager.CurrentPageIndex + "/" + pager.PageCount + "</font>页";
            }
            //mc.ClosePool();
            return table;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
           // GetDataSource(condition);
            //GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text;
           // GridView1.SelectedDataKey.Values[1];
           // DataKeyArray nKey = gvInfo.DataKeys;
      
           int i = 0;
        }

        protected void gvInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            //hidUserID.Value = gvInfo.DataKeys[gvInfo.SelectedIndex].Values["USERID"].ToString();
           // e.Row.ClientID.ToString() + ".checked=true;selectx(this)"
            GridViewRow row = gvInfo.SelectedRow;



        }

        protected void Search_Click(object sender, EventArgs e)
        {
            string keys = searchKey.Text;
            if (keys == "")
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('请输入关键字');", true);
                return;
            }
            condition = "where userName like '%" + keys + "%'";
            GetDataSource(condition);
        }

        protected void Unnamed1_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                
                string userid = gvInfo.DataKeys[gvInfo.SelectedIndex].Values["USERID"].ToString();
                
                EntityOprHelper helper = new EntityOprHelper();

                studentClass userInf = helper.GetEntity<studentClass>(userid);
                if (userInf != null)
                {
                  bool  b=  helper.DelEntity<studentClass>(userInf);
                  if (b)
                  {
                      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('删除完毕');", true);
                      GetDataSource(condition);
                  }
                }


            }
            catch (Exception ex)
            {

            }
        }

        protected void gvInfo_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            //GridViewRow row = gvInfo.Rows[e.NewSelectedIndex];

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
        }

        protected void EditUser_Click(object sender, ImageClickEventArgs e)
        {
            //EditUserWindow
            if (gvInfo.SelectedIndex >= 0)
            {
                string userid = gvInfo.DataKeys[gvInfo.SelectedIndex].Values["USERID"].ToString();
                string sName = gvInfo.Rows[gvInfo.SelectedIndex].Cells[0].Text;
                string sNice = gvInfo.Rows[gvInfo.SelectedIndex].Cells[1].Text;

                string sable = gvInfo.DataKeys[gvInfo.SelectedIndex].Values["DISABLED"].ToString();
                string slock = gvInfo.DataKeys[gvInfo.SelectedIndex].Values["LOCKED"].ToString();
                if (sable == "1")
                    sable = "是";
                else
                    sable = "否";
                if (slock == "1")
                    slock = "是";
                else
                    slock = "否";

               // string sable = gvInfo.Rows[gvInfo.SelectedIndex].Cells[4].Text;
                //string slock = gvInfo.Rows[gvInfo.SelectedIndex].Cells[5].Text;


                string arg = "sName=" + sName + "&sNice=" + sNice + "&userid=" + userid + "&sable=" + sable + "&slock=" + slock;
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "EditUserWindow('EdituserInfo.aspx?" + arg + "');", true);
              //  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('1111222');", true);

            }
        }

        public string ShowString(object str2)
        {
            string strFont = string.Empty;
            string str = str2.ToString();
            if (str == "1")
            {
                strFont = "是";
                return strFont;
            }
            else if (str == "0")
            {
                strFont = "否";
                return strFont;
            }
            else
            {
                strFont = "否";
                return strFont;
            }
        }


    }
}